(function(){
	var AjaxCrud = QualityCheck.AjaxCRUD;
	var serverUrl = QualityCheck.GPUD.ServiceConfig;
	$(function(){
		var usercode = $("#user-name",parent.parent.document).attr("code");
		var userkey = $("#user-name",parent.parent.document).attr("key");
		var usertype = $("#user-name",parent.parent.document).attr("usertype");
			keyCheck(usercode,usertype,userkey,function(){
			initSmPages();
			window.onresize = function () {
				initSmPages();
			};
			pagebtns();
			initStudentMaillist();
		});
	});	
	function initStudentMaillist(){
		$("#student-maillist-page").empty();
		var userKey = $("#user-name",parent.parent.document).attr("key");
		var schoolcode = $("#user-name",parent.parent.document).attr("schoolcode");
		var classcode = $(".classes-choice-select",parent.document).attr("classcode");		
		var obj = {
				schoolcode:schoolcode
		};
		if(classcode != "" && classcode != undefined){
			obj["classcode"] = classcode;
		}
		var data = {
				cmd:"getStudentMailList",
				model:"maillist",
				userKey:userKey,
				data:obj,
				remark:"",
		};
		AjaxCrud.get(serverUrl.ServerLoginUrl,data,function(res){
			var content = "";
			if(res.result == 1  && res.data != undefined){
				var maoAHref = "索引： ";
				var data = res.data;				
				if(data.length > 0){
					for(var i = 0; i < data.length; i++){
						for(var key in data[i]){
							if(data[i][key] == null || data[i][key] == "null" || data[i][key] == undefined){
								data[i][key] = "";
							}
						}
						var groupName = data[i].groupName;
						var studentgroup = data[i].studentgroup;
						for(var j = 0; j < studentgroup.length; j++){
							if(j == 0){
								maoAHref += " <a href='#"+groupName+j+"' style=\"color:#fff;\"> " + groupName.toUpperCase() + " </a> ";
							}
							for(var key in studentgroup[j]){
								if(studentgroup[j][key] == null || studentgroup[j][key] == "null"){
									studentgroup[j][key] = "";
								}
							}	
							content += "<div class=\"table-trs\">" +
							"<div class=\"tables-left\" style='margin-left:15px;width:120px;'>" +
							"<div class=\"table-name\" style='width:220px;' id='"+studentgroup[j].groupname+j+"'>"+ studentgroup[j].studentphone + "</div>" +
							"<div class=\"table-code\" style='width:220px;'>学生姓名： "+ studentgroup[j].studentname +"</div>" +
							"</div>" +
							"<div class=\"tables-middle\">" +
							"<div class=\"table-class\" style='width:220px;'>家长电话："+studentgroup[j].userphone+"</div>" +
							"<div class=\"table-code\" style='width:220px;'>家长姓名："+studentgroup[j].username+"</div>" +
							"</div>" +
							"<div class=\"tables-right\">" +
							"<div class=\"table-address\"></div>" +
							"<div class=\"table-address\"></div>" +
							"</div>" +
							"</div>";
						}
						
					}
					$("#student-maillist-data-show").empty();
					$("#student-maillist-data-show").append(content);
					
					$("#student-maillist-page").empty();
					$("#student-maillist-page").append(maoAHref);
				}else{
					content += "<div class=\"table-trs\">" +
									"<div class=\"no-data-show\">没有查到相关结果！</div>" +
								"</div>";
					$("#student-maillist-data-show").empty();
					$("#student-maillist-data-show").append(content);
				}
			}else{
				content += "<div class=\"table-trs\">" +
								"<div class=\"no-data-show\">没有查到相关结果！</div>" +
							"</div>";
				$("#student-maillist-data-show").empty();
				$("#student-maillist-data-show").append(content);
			}
		},null,null);	
	}

	function substr(str,len){
		if(str != undefined){
			if(str.length > len){
				str = str.substring(0,len-2)+"...";
				return str;
			}else{
				return str;
			}
		}else{
			return "";
		}
	}
	
	function pagebtns(){
		$(".classes-choice",parent.document).unbind("click");
		$(".classes-choice",parent.document).click(function(){
			$(".classes-choice",parent.document).removeClass("classes-choice-select");
			$(this).addClass("classes-choice-select");
			initStudentMaillist();
			
		});	
	}
	function initSmPages(){
		var rcH = $(".body-right",parent.document).height() - $(".right-title").height()-3;
		$(".right-content").height(rcH);
		
		var cdsH = $(".right-content").height() - $(".content-title").height() - 40;
		$(".content-data-show").height(cdsH);
	}
})();